package com.zzq.calcgame;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zzq.calcgame.entity.Question;
import com.zzq.calcgame.model.ExamManager;
import com.zzq.calcgame.storage.UserSetting;
import com.zzq.calcgame.widget.CalcEditText;

import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    ArrayList<Question> questions=new ArrayList<>();

    Button preButton,nextButton;
    TextView tips,sysAnswer;//当前题号


    CalcEditText num1,num2,answer;
    TextView operator;//操作符

    int currentIndex=0;

    UserSetting setting;


    int state=1;//1 做题 2.浏览

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_bar_main);

        initView();
        setting=new UserSetting(this);

        state=getIntent().getIntExtra("MODE",1);
        initExam();
        if(state!=1)
            setTitle("查看答题记录");
        else{
            setTitle("做题目");
        }
    }

    private void initExam() {
        currentIndex=0;
        if(state==1){
            questions.addAll(ExamManager.getInstance(this).cratePaper(setting.getQuestionCount()));
        }else{
            answer.setEnabled(false);
            questions.addAll(ExamManager.getInstance(this).getQuestionByPaperId(getIntent().getIntExtra("paperId",0)));
        }
        moveToIndex(currentIndex);
    }
    Toolbar toolbar;
    private void initView() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        preButton= (Button) findViewById(R.id.pre);
        nextButton= (Button) findViewById(R.id.next);
        tips= (TextView) findViewById(R.id.tips);
        num1= (CalcEditText) findViewById(R.id.number1);
        num2= (CalcEditText) findViewById(R.id.number2);
        answer= (CalcEditText) findViewById(R.id.answer);
        operator= (TextView) findViewById(R.id.operator);
        sysAnswer= (TextView) findViewById(R.id.sys_answer);
    }

    private void moveToIndex(int index){
        preButton.setEnabled(index>0);
        nextButton.setText(index>=questions.size()-1?state==1?"交卷":"确定":"下一题");
        if(index>=0&&index<questions.size()){
            Question item=questions.get(index);
            num1.setText(item.getNumber1());
            num2.setText(item.getNumber2());
            operator.setText(item.getOperator().getName());
            tips.setText(String.format(Locale.getDefault(),"%d/%d",index+1,questions.size()));
            if(state==1)
                questions.get(currentIndex).setAnswer(answer.getText().toString());
            else{
                answer.setText(item.getAnswer());
                int type=item.getOperator().getType();
                float result;
                int num1 = Integer.parseInt(item.getNumber1());
                int num2 = Integer.parseInt(item.getNumber2());
                if(type==0) {
                    result= num1 + num2;
                }else if (type==1){
                    result=num1-num2;
                }else if(type==2){
                    result=num1*num2;
                }else{
                    result=((float)num1)/num2;
                }
                sysAnswer.setText("正确答案:"+result);
            }
            currentIndex=index;



        }
    }


    public void onClick(View view){
        switch (view.getId()){
            case R.id.pre:
                moveToIndex(currentIndex-1);
                break;
            case R.id.next:
                if(state==1){
                    if(currentIndex>=questions.size()-1&&!questions.isEmpty()){
                        if(ExamManager.getInstance(this).submit(questions)){
                            questions.clear();
                            AlertDialog.Builder builder=new AlertDialog.Builder(this);
                            builder.setMessage("交卷成功,再试试吧").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    initExam();
                                }
                            }).setCancelable(false).show();
                        }else{
                            Toast.makeText(this,"交卷失败",Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        moveToIndex(currentIndex+1);
                    }
                }else{
                    if(currentIndex>=questions.size()-1){
                        finish();
                    }else{
                        moveToIndex(currentIndex+1);
                    }


                }


                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            startActivity(new Intent(this,RecordActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
